package com.coolgor.axnut.web.airuc;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.airvow.Net.HttpServletRequestUtil;
import com.coolgor.axnut.dto.airuc.CourseExecution;
import com.coolgor.axnut.dto.auth.LoginInfoExecution;
import com.coolgor.axnut.entity.airuc.College;
import com.coolgor.axnut.entity.airuc.Course;
import com.coolgor.axnut.entity.airuc.Lesson;
import com.coolgor.axnut.entity.auth.LoginInfo;
import com.coolgor.axnut.service.airuc.CollegeService;
import com.coolgor.axnut.service.airuc.CourseService;
import com.coolgor.axnut.service.airuc.LessonService;


@Controller
@RequestMapping(value = "airuc", method = { RequestMethod.GET, RequestMethod.POST })
public class CourseManagementController {
	@Autowired
	private CourseService courseService;
	
	@RequestMapping(value = "/getcoursebyid", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getCourseById(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Long courseID = HttpServletRequestUtil.getLong(request, "courseid");
		if (courseID > -1) {
			try {
				Course course = courseService.getCourse(courseID);
				modelMap.put("course", course);
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty courseID");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getCourseListByCollegeID", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getCourseListByCollegeID(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
	    int page = HttpServletRequestUtil.getInt(request, "page");
	    int pageSize=HttpServletRequestUtil.getInt(request, "rows");
		Course courseCondition = new Course();
		Long collegeID = HttpServletRequestUtil.getLong(request, "collegeid");
		if (null!=collegeID) {
			courseCondition.setCollegeID(collegeID);			
		}
		CourseExecution exe = courseService.getCourseList(courseCondition, page, pageSize);
		if (null != exe ) {
			try {
				modelMap.put("total",exe.getCount());
				modelMap.put("rows",exe.getCourseList());
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty List");
		}
		return modelMap;
	}	
	
}
